clear all
clc
close all

load basicgame

%% Parameter grid

lambda=0:0.01:0.2;

%% Run cross-validation

K=10;
n=length(y);
c = cvpartition(n,'KFold',K);
bestlambda=cell(1,K);
errLogit=zeros(1,K);
errTL=zeros(1,K);
empdistr=cell(1,K);
testx=cell(1,K);
testy=cell(1,K);
idTest=cell(1,K);
VarDiffLL1=zeros(1,K);
VarDiffU=zeros(1,K);
Covar=zeros(1,K);

for k=1:K

    % Training Data
    
    train=find(c.training(k));
    trainy=y(train,:);
    trainx=x(train);
    
    bestlambda{k}=logitlev1(lambda,trainx,trainy);
    
    % Table Lookup
    
    idTrain=id(train);
    r=length(unique(id));
    empdistr{k}=zeros(r,3);
    if r~=466
        'WARNING'
    else
        for i=1:r
            gamesemp=find(idTrain==i);
            actemp=trainy(gamesemp);
            empdistr{k}(i,:)=[length(find(actemp=='1')),length(find(actemp=='2')),length(find(actemp=='3'))];
            empdistr{k}(i,:)=empdistr{k}(i,:)/sum(empdistr{k}(i,:));
            clear gamesemp
        end
    end      
    
    % Test Data
    
    test=find(c.test(k));
    testy{k}=y(test,:);
    testx{k}=x(test);
    idTest{k}=id(test);
    
    L=bestlambda{k};
    [errTL(k),errLogit(k),VarDiffLL1(k),VarDiffU(k),Covar(k)]=combinederrLL1(lambda(L(1)),testx{k},testy{k},idTest{k},empdistr{k});

end

%% Print 

'Best Lambda'
lambda(bestlambda{1}) %vary to see best-fit estimates of tau in different folds of CV

'Logit Lev 1 Error'
mean(errLogit)

'Logit Lev 1 SE'
sqrt(var(errLogit))/sqrt(K) 

'Table Lookup Error'
mean(errTL) 

'Completeness'
kappa = (-log(1/3)-mean(errLogit))/(-log(1/3)-mean(errTL)) 

'Standard Error, Completeness'
se=sqrt((mean(VarDiffLL1) - 2*kappa*mean(Covar) + kappa^2*mean(VarDiffU)) / (-log(1/3)-mean(errTL))^2) * (1/sqrt(n))
